const db=wx.cloud.database().collection('user')
const app = getApp()
Page({
    data:{
        userInfo:null,
        visible2: false,
        signOk:false,
        show: false,
        info:''

    },
    onLoad(){
   
    },
   async login(){
       //1.用户授权信息
     let{ userInfo:{nickName,avatarUrl,_id} } =  await wx.getUserProfile({
                   desc: '进行授权登录方可答题',
                })
       //2.把获取的信息给后端       

        const {result: {data} }= await wx.cloud.callFunction({
                 name: "comlogin",
                 _id: _id,
                 data:{
                     nickName,
                     avatarUrl
                 }
              })
              console.log(data)
        //3.将用户信息进行存储
              wx.setStorageSync('userInfo', data)
              this.setData({
                  userInfo:data
              })
    },
    //获取用户信息
   async getUserInfo(){
        //判断是否登录
        const data = wx.getStorageSync('userInfo')
        if(data){
            //登录了更新数据
            console.log('122',data)
            const userInfos = await db.doc(data._id).get()
            this.setData({
                userInfo:userInfos.data
            })
        }
    },
    onShow(){
        this.getUserInfo()
        let signnow = wx.getStorageSync('singOk')
        this.setData({
            signOk:signnow
        })
         this.sign()
    },
    
    handleOpen2 () {
        this.setData({
            visible2: true
        });
        this.logout()
    },

    handleClose2 () {
        this.setData({
            visible2: false
        });
    },

  async  logout(){
      let that =this;
       await  wx.showModal({
            title: '退出登录',
            content: '是否退出登录',
            success (res) {
               if (res.confirm) {
                    that.setData({
                        userInfo:null
                      })
                   wx.setStorageSync('userInfo',null)
                  } else if (res.cancel) {
                  // console.log('用户点击取消')
              }
            }
        })
    },
 
   async sign(){
        let nowtime = new Date().getTime()
       const beforeTime = wx.getStorageSync('time')
       let onlytime = nowtime-beforeTime
       console.log(onlytime)
       if(onlytime>=54000000){
           wx.setStorageSync('time',nowtime)
           wx.setStorageSync('singOk',false)
           
        }
        this.data.signOk =await wx.getStorageSync('singOk')
        console.log(this.data.signOk)
    
      },
       clickSign(){
        let singStatu = wx.getStorageSync('singOk')
        console.log('fsdjkfjklsdfjk',singStatu)
        if(singStatu===true){
          wx.showToast({
                icon:'none',
                title: '今日已经签到',
            })
        }else if(singStatu===false){
          this.addScoreToUser()
          wx.setStorageSync('singOk',true)
    
            wx.showToast({
                icon:'none',
                title: '积分+3',
              })
           let signnow =  wx.getStorageSync('singOk')
            this.setData({
                signOk :signnow
            })
            this.onShow()

    
        }
    
    
      },
  async addScoreToUser(){
    
    // 获取原来分数   
    let user =  wx.getStorageSync('userInfo')
    console.log(user._openid)
    let {data} = await db.where({
        _openid:user._openid
    }).get()

    //签到+3分
    let score = data[0].score + 3

    //添加分数到数据库
    await db.where({
        _openid:user._openid
    }).update({
       data:{
         score
       }
    })

    },
    updateInfo(info){
        let userinfo = wx.getStorageSync('userInfo');
        db.doc(userinfo._openid)
        .update({
           data:{
            info
           }
        })
        .then(res => {
            console.log("成功")
            this.onShow()
        }).catch(err => {
            console.log("失败")
        })
         
   
    
    },
    showPopup() {
        this.setData({ show: true });
      },
    
      onClose() {
        this.setData({ show: false });
      },
      pleaseLogin(){
          wx.showToast({
            icon:'none',
            title: '登录后可进行操作',
          })
      },
      getToInfo(e){
        console.log(e.detail.value)
        this.setData({
             info : e.detail.value
        })
      },
      addInfo(){
        let info = this.data.info
        if(info.length < 4){
            wx.showToast({
              icon:'none',
              title: '字数要大于5个',
            })
            return
        }
        
        let userinfo = wx.getStorageSync('userInfo');
        db.where({
            _openid:userinfo._openid
        })
        .update({
           data:{
            info
           }
        })
        .then(res => {
            console.log("成功")
            wx.showToast({
                icon:'none',
                title: '修改成功',
              })
        }).catch(err => {
            console.log("失败")
        })
        this.onClose()
        this.onShow()
      }

})